package com.crm.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import com.crm.model.*;
import com.crm.service.EquipmentService;
import com.crm.service.EquipmentTypeService;
import com.crm.service.RepairService;
import com.crm.util.PageUtil;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping("/equipment")
public class EquipmentController {

    @Resource
    private EquipmentService equipmentService;

    @Resource
    private EquipmentTypeService equipmentTypeService;

    @Resource
    private RepairService repairService;

    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", required = false) String page, Equipment s_equipment, HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        HttpSession session = request.getSession();
        if (StringUtils.isEmpty(page)) {
            page = "1";
            session.setAttribute("s_equipment", s_equipment);
        } else {
            s_equipment = (Equipment) session.getAttribute("s_equipment");
        }
        PageBean pageBean = new PageBean(Integer.parseInt(page), 3);
        List<Equipment> equipmentList = equipmentService.list(pageBean, s_equipment);
        int total = equipmentService.count(s_equipment);
        String pageCode = PageUtil.getPagination(request.getContextPath() + "/equipment/list.do", total, Integer.parseInt(page), 3);
        mav.addObject("pageCode", pageCode);
        mav.addObject("modeName", "设备管理");
        mav.addObject("equipmentList", equipmentList);
        mav.addObject("mainPage", "equipment/list.jsp");
        mav.setViewName("main");
        return mav;
    }


    @RequestMapping("/delete")
    public void delete(@RequestParam(value = "id") String id, HttpServletResponse response) throws Exception {
        JSONObject result = new JSONObject();
        equipmentService.delete(Integer.parseInt(id));
        result.put("success", true);

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(result.toString());
        out.flush();
        out.close();
    }

    @RequestMapping("/preSave")
    public ModelAndView preSave(@RequestParam(value = "id", required = false) String id) {
        ModelAndView mav = new ModelAndView();
        List<EquipmentType> equipmentTypeList = equipmentTypeService.list(null, null);
        mav.addObject("mainPage", "equipment/save.jsp");
        mav.addObject("modeName", "设备管理");
        mav.addObject("equipmentTypeList", equipmentTypeList);
        mav.setViewName("main");
        if (StringUtils.isNotEmpty(id)) {
            mav.addObject("actionName", "设备修改");
            Equipment equipment = equipmentService.loadById(Integer.parseInt(id));
            mav.addObject("equipment", equipment);
        } else {
            mav.addObject("actionName", "设备添加");
        }
        return mav;
    }

    @RequestMapping("/save")
    public String save(Equipment equipment) {
        if (equipment.getId() == null) {
            equipmentService.add(equipment);
        } else {
            equipmentService.update(equipment);
        }
        return "redirect:/equipment/list.do";
    }

    @RequestMapping("/usingEquipmentList")
    public ModelAndView usingEquipmentList(@RequestParam(value = "page", required = false) String page, Equipment s_equipment, HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        HttpSession session = request.getSession();
        if (StringUtils.isEmpty(page)) {
            page = "1";
            session.setAttribute("s_equipment", s_equipment);
        } else {
            s_equipment = (Equipment) session.getAttribute("s_equipment");
        }
        PageBean pageBean = new PageBean(Integer.parseInt(page), 3);
        List<Equipment> equipmentList = equipmentService.list(pageBean, s_equipment);
        int total = equipmentService.count(s_equipment);
        String pageCode = PageUtil.getPagination(request.getContextPath() + "/equipment/userList.do", total, Integer.parseInt(page), 3);
        mav.addObject("pageCode", pageCode);
        mav.addObject("modeName", "使用设备管理");
        mav.addObject("equipmentList", equipmentList);
        mav.addObject("mainPage", "equipment/usingEquipmentList.jsp");
        mav.setViewName("main");
        return mav;
    }

    @RequestMapping("/repair")
    public void repair(@RequestParam(value = "id") String id, HttpSession session, HttpServletResponse response) throws Exception {
        String userMan = ((User) session.getAttribute("currentUser")).getUserName();
        JSONObject result = new JSONObject();
        equipmentService.addRepair(Integer.parseInt(id), userMan);
        result.put("success", true);

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(result.toString());
        out.flush();
        out.close();
    }

    @RequestMapping("/repairEquipmentList")
    public ModelAndView repairEquipmentList(@RequestParam(value = "page", required = false) String page, Repair s_repair, HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        HttpSession session = request.getSession();
        if (StringUtils.isEmpty(page)) {
            page = "1";
            session.setAttribute("s_repair", s_repair);
        } else {
            s_repair = (Repair) session.getAttribute("s_repair");
        }
        s_repair.setFinishState(1);
        PageBean pageBean = new PageBean(Integer.parseInt(page), 3);
        List<Repair> repairList = repairService.list(pageBean, s_repair);
        int total = repairService.count(s_repair);
        String pageCode = PageUtil.getPagination(request.getContextPath() + "/equipment/repairEquipmentList.do", total, Integer.parseInt(page), 3);
        mav.addObject("pageCode", pageCode);
        mav.addObject("modeName", "维修设备管理");
        mav.addObject("repairList", repairList);
        mav.addObject("mainPage", "equipment/repairEquipmentList.jsp");
        mav.setViewName("main");
        return mav;
    }


    @RequestMapping("/finishRepair")
    public void finishRepair(@RequestParam(value = "id") String id, @RequestParam(value = "repairId") String repairId, @RequestParam(value = "success") boolean success, HttpSession session, HttpServletResponse response) throws Exception {
        String repairMan = ((User) session.getAttribute("currentUser")).getUserName();
        JSONObject result = new JSONObject();
        equipmentService.updateRepair(Integer.parseInt(id), Integer.parseInt(repairId), repairMan, success);
        result.put("success", true);

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(result.toString());
        out.flush();
        out.close();
    }

    @RequestMapping("/repairHistoryEquipmentList")
    public ModelAndView repairHistoryEquipmentList(@RequestParam(value="page",required=false)String page,Repair s_repair,HttpServletRequest request){
        ModelAndView mav=new ModelAndView();
        HttpSession session=request.getSession();
        if(StringUtils.isEmpty(page)){
            page="1";
            session.setAttribute("s_repair", s_repair);
        }else{
            s_repair=(Repair) session.getAttribute("s_repair");
        }
        s_repair.setFinishState(2);
        PageBean pageBean=new PageBean(Integer.parseInt(page),3);
        List<Repair> repairList=repairService.list(pageBean, s_repair);
        int total=repairService.count(s_repair);
        String pageCode=PageUtil.getPagination(request.getContextPath()+"/equipment/repairHistoryEquipmentList.do", total, Integer.parseInt(page), 3);
        mav.addObject("pageCode", pageCode);
        mav.addObject("modeName", "维修设备历史");
        mav.addObject("repairList", repairList);
        mav.addObject("mainPage", "equipment/repairHistoryEquipmentList.jsp");
        mav.setViewName("main");
        return mav;
    }
}
